import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class TestFrame1 extends JFrame implements ActionListener {
	JButton redButton;
	JButton grnButton;
	JButton bluButton;
	JButton blkButton;

	// constructor for TwoButtons
	public TestFrame1(String title) {
		super(title);

		redButton = new JButton("red");
		grnButton = new JButton("green");
		bluButton = new JButton("blue");
		blkButton = new JButton("black");

		redButton.setActionCommand("red"); // set the command
		grnButton.setActionCommand("green"); // set the command
		bluButton.setActionCommand("blue");
		blkButton.setActionCommand("black");

		// register the buttonDemo frame
		// as the listener for both Buttons.
		redButton.addActionListener(this);
		grnButton.addActionListener(this);
		bluButton.addActionListener(this);
		blkButton.addActionListener(this);

		setLayout(new FlowLayout());
		add(redButton);
		add(grnButton);
		add(bluButton);
		add(blkButton);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void actionPerformed(ActionEvent evt) {
		// check which command has been sent
		switch (evt.getActionCommand()) {
		case "red":
			getContentPane().setBackground(Color.red);
			break;
		case "green":
			getContentPane().setBackground(Color.green);
			break;
		case "blue":
			getContentPane().setBackground(Color.blue);
			break;
		case "black":
			getContentPane().setBackground(Color.black);
			break;

		default:
			break;
		}

		repaint();
	}

	public static void main(String[] args) {
		TestFrame1 demo = new TestFrame1("Click a Button");

		demo.setSize(200, 150);
		demo.setVisible(true);
	}
}